package ua.org.enginer.remoting.services
{
	import flash.utils.ByteArray;
	
	import mx.rpc.events.ResultEvent;
	
	import ua.org.enginer.remoting.events.RemoteResultEvent;
	import ua.org.enginer.remoting.vo.RemoteResult;
	import ua.org.enginer.remoting.vo.UploadResultStatuses;
	
	[Event(name='complete', type='ua.org.enginer.remoting.events.RemoteResultEvent')]
	[Event(name='error', type='ua.org.enginer.remoting.events.RemoteResultEvent')]
	
	/**
     * Class that extends the RemoteService class, therefore it makes use of the default error handling for
     * remote calls.
     */
	public class RemoteFileService extends RemoteService {
		private static var phpServiceClass:String = "RemoteFile";
		
		public function RemoteFileService(/*amfChannelId:String, amfChannelEndpoint:String*/) {
			super("remotefileService", "amfphp", "amfphp", "amfphp/gateway.php");
		}
        
        public function upload(fileData:ByteArray, path:String, type:String):void {
        	remoteObject.source = phpServiceClass;
        	remoteObject.upload.addEventListener(ResultEvent.RESULT,handleRemoteMethod);
            remoteObject.upload(fileData, path, type);
        }
        
        protected function handleRemoteMethod(event:ResultEvent):void {
        	var result:RemoteResult = new RemoteResult(event.result)
        	if (result.status == UploadResultStatuses.COMPLETE)
        		dispatchEvent(new RemoteResultEvent(RemoteResultEvent.COMPLETE, result))
        	else if (result.status == UploadResultStatuses.ERROR)	
        		dispatchEvent(new RemoteResultEvent(RemoteResultEvent.ERROR, result))
        }
	}
}